/**
 * 
 */
package edu.arizona.bio5.onto.decomposition;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;

import uk.ac.manchester.cs.owlapi.modularity.ModuleType;

import edu.arizona.bio5.onto.util.OntologyIterator;

/**
 * Runs a specific ontology processing task on all ontologies in a folder
 * 
 * @author Pavel Klinov
 *
 */
public class AllOntologiesEvaluator {

	private static final String BIOPORTAL_FOLDER = "eval/onto/bioportal";
	private static final String CSV_FILE = "eval/bioportal_ad_fme_eval.csv"; 
	
	private static OntologyProcessingTaskRunner m_taskRunner = new XMLDBPersistingTaskRunner(	ModuleType.BOT,
																								"xmldb:exist://localhost:8080/exist/xmlrpc/db/BioPortal",
																								"admin",
																								"");
	
	
	public static void main(String[] args) throws Exception {
		
		OntologyIterator iter = new OntologyIterator(BIOPORTAL_FOLDER);
		File csvFile = new File(CSV_FILE);
		
		if (csvFile.exists()) {
			
			csvFile.delete();
		}
			
		csvFile.createNewFile();
		
		BufferedWriter writer = new BufferedWriter(new FileWriter(csvFile));
		
		while (iter.hasNext()) {
			
			OWLOntology ontology = iter.next();
			OWLOntologyManager mgr = iter.getCurrentManager();
			
			if (ontology != null) {
				
				System.out.print("Processing... ");
				
				writer.write(iter.getCurrentOntologyFile() + ",");
				
				long ts = System.currentTimeMillis();
				
				try {
					
					m_taskRunner.setOntologyFile(iter.getCurrentOntologyFile());
					m_taskRunner.process(ontology, mgr, writer);
					
				} catch (Exception e) {
					
					writer.write("Exception: " + e.getMessage());
					writer.newLine();
					continue;
				}
				
				ts = System.currentTimeMillis() - ts;
				
				System.out.println(" done in " + ts + " ms");

				writer.newLine();				
				writer.flush();
			}
		}
		
		writer.close();
	}
}